/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.model;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public abstract class AbstractHTTPCommunicator implements Runnable {
	protected ThreadListener threadListener;
	protected ProgressListener progListener;
	protected String response;
	protected String request;
	protected String statusMessage;
	protected String connectionURL;

	/**
	 * 
	 * @param url
	 *            to which the connection should be established
	 * @param connectionType
	 *            either HttpConnection.POST or -GET
	 * @return
	 * @throws IOException
	 */
	protected HttpConnection createConnection(String connectionType)
			throws IOException {
		HttpConnection hc = null;
		hc = (HttpConnection) Connector.open(connectionURL);
		hc.setRequestMethod(connectionType);

		// if(connectionType == HttpConnection.POST){
		// // hc.setRequestProperty("User-Agent",
		// System.getProperty("microedition.profiles"));
		// hc.setRequestProperty("Content-Type",
		// "application/xml; charset=iso-8859-1");
		// hc.setRequestProperty("Content-Length",
		// Integer.toString(requestXML.length()));
		// }
		return hc;
	}

	protected void notifyListener() {
		if (this.threadListener != null)
			this.threadListener.threadFinished();
	}

	public String getConnectionURL() {
		return connectionURL;
	}

	public void setConnectionURL(String connectionURL) {
		this.connectionURL = connectionURL;
	}

	public String getStatusMessage() {
		return statusMessage;
	}

	public void setStatusMessage(String statusMessage) {
		this.statusMessage = statusMessage;
	}

	public void removeThreadListener() {
		this.threadListener = null;
	}

	public void setThreadListener(ThreadListener threadListener) {
		this.threadListener = threadListener;
	}

	public ProgressListener getProgListener() {
		return progListener;
	}

	public void setProgListener(ProgressListener progListener) {
		this.progListener = progListener;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public String getRequest() {
		return request;
	}

	public void setRequest(String request) {
		this.request = request;
	}

}
